import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:intl/intl.dart';
import 'package:seedfour/app/constants.dart';
import 'package:seedfour/app/theme.dart';
import 'package:seedfour/app/views/module_main/main_home/state.dart';
import 'package:seedfour/toolkit/typedef/function.dart';

import '../../../../models/notice_v2_list_entity.dart';
import '../../../../widget/tag.dart';

class HomeCase extends StatelessWidget {
  final HomeState data;
  final ParamVoidCallback onClickMore;
  final ParamSingleCallback onClickItem;

  const HomeCase({
    Key? key,
    required this.data,
    required this.onClickMore,
    required this.onClickItem,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        // Padding(
        //   padding: EdgeInsets.symmetric(horizontal: kSpaceSmall),
        //   child: _buildTitle(),
        // ),
        SizedBox(height: kSpaceMini),
        Obx(
          () => Column(
            children: [
              ...data.noticeList
                  .map((element) => Container(
                        margin: EdgeInsets.symmetric(
                            horizontal: kSpaceMedium, vertical: kSpaceTiny),
                        child: Ink(
                          decoration: BoxDecoration(
                            color: Colors.white,
                            borderRadius: BorderRadius.circular(kRadiusSmall),
                          ),
                          child: InkWell(
                            borderRadius: BorderRadius.circular(kRadiusSmall),
                            onTap: () => onClickItem(element),
                            child: Padding(
                              padding: EdgeInsets.all(kSpaceSmall),
                              child: _buildCaseItem(element),
                            ),
                          ),
                        ),
                      ))
                  .toList()
            ],
          ),
        ),
      ],
    );
  }

  Widget _buildCaseItem(NoticeV2Data item) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            // 案件标题
            Expanded(
              child: Text(
                item.title ?? '',
                maxLines: 1,
                overflow: TextOverflow.ellipsis,
                style: TextStyle(
                  color: AppColor.colorFontBlack,
                  fontSize: kFontSizeMedium,
                  fontWeight: AppFont.fontMedium,
                ),
              ),
            ),
            SizedBox(width: kSpaceMini),
            // 案源金额
            Text(
              item.subjectAmount ?? '',
              maxLines: 1,
              overflow: TextOverflow.ellipsis,
              style: TextStyle(
                color: AppColor.colorFontBlue,
                fontSize: kFontSizeMedium,
                fontWeight: AppFont.fontMedium,
              ),
            )
          ],
        ),

        SizedBox(height: kSpaceTiny),

        Row(
          children: [
            Text(
              item.name,
              style: TextStyle(
                color: AppColor.colorFontBlack,
                fontSize: kFontSizeMini,
              ),
            ),
            SizedBox(width: kSpaceMini),
            Text(
              DateFormat('yyyy.MM.dd')
                  .format(DateTime.fromMillisecondsSinceEpoch(item.createdAt!)),
              style: TextStyle(
                color: AppColor.colorFontBlack,
                fontSize: kFontSizeMini,
              ),
            ),
          ],
        ),

        SizedBox(height: kSpaceSmall),

        // 案源内容
        Text(
          item.content ?? '',
          maxLines: 3,
          overflow: TextOverflow.ellipsis,
          style: TextStyle(
            color: AppColor.colorFontGrey,
            fontSize: kFontSizeMini,
            fontWeight: AppFont.fontRegular,
          ),
        ),

        if (item.subjectAmount != null &&
            item.subjectAmount!.isNotEmpty &&
            item.cooperRatio != 0)
          SizedBox(height: kSpaceSmall),

        Wrap(
          spacing: kSpaceSmall,
          runSpacing: kSpaceTiny,
          children: [
            // 标的额
            item.subjectAmount.isBlank ?? true
                ? Container()
                : Container(
                    decoration: BoxDecoration(
                      color: AppColor.colorTagBackground,
                      borderRadius: BorderRadius.circular(kRadiusMini),
                    ),
                    padding: EdgeInsets.symmetric(
                        horizontal: kSpaceTiny, vertical: 2.w),
                    child: Text(
                      '标额${item.subjectAmount}',
                      style: TextStyle(
                          color: AppColor.colorFontBlack,
                          fontSize: kFontSizeMini),
                    ),
                  ),

            // 案源比例
            item.cooperRatio == 0
                ? Container()
                : Container(
                    decoration: BoxDecoration(
                      color: AppColor.colorTagBackground,
                      borderRadius: BorderRadius.circular(kRadiusMini),
                    ),
                    padding: EdgeInsets.symmetric(
                        horizontal: kSpaceTiny, vertical: 2.w),
                    child: Text(
                      '案源比例${item.cooperRatio}%',
                      style: TextStyle(
                          color: AppColor.colorFontBlack,
                          fontSize: kFontSizeMini),
                    ),
                  ),
          ],
        ),

        SizedBox(height: kSpaceMini),

        Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Text(
              DateFormat('yyyy.MM.dd')
                  .format(DateTime.fromMillisecondsSinceEpoch(item.createdAt!)),
              style: TextStyle(
                color: AppColor.colorFontGrey,
                fontSize: kFontSizeMini,
                fontWeight: AppFont.fontRegular,
              ),
            ),
            Tag(
              text: switch (item.status) {
                0 => '未领取',
                1 => '已领取',
                2 => '已成交',
                int() => throw UnimplementedError(),
                null => throw UnimplementedError(),
              },
              textColor: switch (item.status) {
                0 => AppColor.colorFontOrange,
                1 => AppColor.colorFontGrey,
                2 => AppColor.colorFontGreen,
                int() => throw UnimplementedError(),
                null => throw UnimplementedError(),
              },
              textSize: kFontSizeMini,
              borderColor: switch (item.status) {
                0 => AppColor.colorFontOrange,
                1 => AppColor.colorFontGrey,
                2 => AppColor.colorFontGreen,
                int() => throw UnimplementedError(),
                null => throw UnimplementedError(),
              },
              padding:
                  EdgeInsets.symmetric(horizontal: kSpaceTiny, vertical: 2.w),
            ),
          ],
        )
      ],
    );
  }
}
